<template>
  <div class="container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>管理员登录</span>
          <!-- <el-button class="button" text>Operation button</el-button> -->
        </div>
      </template>
      <!-- 登录表单 -->
      <el-form
        ref="ruleFormRef"
        :model="userForm"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="userForm.username"
            type="text"
            autocomplete="off"
            placeholder="请输入账号"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="userForm.password"
            type="password"
            autocomplete="off"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"
            >登录</el-button
          >
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
// 导入store
import useLoginStore from "../../stores/login.js";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

const router = useRouter();
// 声明store对象
const login = useLoginStore();

const ruleFormRef = ref();

// 登录表单
const userForm = reactive({
  username: "",
  password: "",
});

const rules = reactive({
  username: [
    { required: true, message: "账号不能为空", trigger: "blur" },
    {
      min: 4,
      max: 18,
      message: "长度在 4 到 18 个字符",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "密码不能为空", trigger: "blur" },
    {
      min: 6,
      max: 8,
      message: "密码长度 6~18个字符",
      trigger: "blur",
    },
  ],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      // console.log("submit!");
      // console.log("console", userForm);
      login.loginActions(userForm).then((res) => {
        // console.log("登录结果-----------", res);
        if (res.code === 200) {
          login.INIT_LOGINUSER(res.list);
          login.INIT_TOKEN(res.list.token);
          ElMessage({
            type: "success",
            message: "登录成功",
          });
          // 跳转页面
          router.push({ name: "home" });
        } else {
          ElMessage({
            type: "error",
            message: res.msg,
          });
        }
      });
    } else {
      console.log("error submit!");
      return false;
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style lang="css" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 480px;
}
</style>
